package com.qyer.search.model.lastminute;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.qyer.search.Constants;
import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.filter.AbstractFilter;
import com.qyer.search.filter.AbstractIDFilter;
import com.qyer.search.filter.AbstractValidator;
import com.qyer.search.model.SearchParameterEntity;
import com.qyer.search.model.aggregation.AggregationProduct;
import com.qyer.search.model.aggregation.AggregationRegular;
import com.qyer.search.model.aggregation.City;
import com.qyer.search.model.aggregation.Country;
import com.qyer.search.model.aggregation.Poi;
import com.qyer.search.model.aggregation.ProductProject;
import com.qyer.search.model.aggregation.STGInfo;
import com.qyer.search.model.aggregation.SetOutPlace;
import com.qyer.search.model.aggregation.Topic;
import com.qyer.search.model.aggregation.TravelDay;
import com.qyer.search.model.aggregation.VStype;
import com.qyer.search.model.aggregation.VTYPEDES;
import com.qyer.search.model.mutable.IndexMap;
import com.qyer.search.model.mutable.Place;
import com.qyer.search.model.mutable.ProductSnapshot;
import com.qyer.search.model.mutable.StrictPlace;
import com.qyer.search.utils.DestinationUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by tangzheng on 16/8/11.
 */
public class SimpleZSKU extends ZSKU {

  @JsonProperty(FIELD_OUT_ID)
  private int id;
  @JsonIgnore
  private Set<Integer> type1 = new HashSet<>();
  @JsonProperty(FIELD_OUT_TYPE2)
  private Set<Integer> type2 = new HashSet<>();
  @JsonIgnore
  private int market_info;
  @JsonProperty
  String title;

  public SimpleZSKU(int id) {
    this.id = id;
  }

  @Override public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  @Override public Set<Integer> getType1() {
    return type1;
  }

  public void setType1(Set<Integer> type1) {
    this.type1 = type1;
  }

  @Override public Set<Integer> getType2() {
    return type2;
  }

  public void setType2(Set<Integer> type2) {
    this.type2 = type2;
  }

  @Override public int getMarket_info() {
    return market_info;
  }

  public void setMarket_info(int market_info) {
    this.market_info = market_info;
  }

  @Override public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public void addType1 (int t){
    this.type1.add(t);
  }

  public void addType2 (int t){
    this.type2.add(t);
  }

  public SimpleZSKU type1(int t) {
    if(CollectionUtils.isEmpty(type1)) {
      setType1(new HashSet<Integer>());
      addType1(t);
    }else {
      addType1(t);
    }
    return this;
  }

  public SimpleZSKU type2(int t) {
    if(CollectionUtils.isEmpty(type2)) {
      setType2(new HashSet<Integer>());
      addType2(t);
    }else {
      addType2(t);
    }
    return this;
  }

  private static SimpleZSKU build(int id) {
    return new SimpleZSKU(id);
  }

  public static SimpleZSKU build(SearchParameterEntity spe, String data,
                                 List<AbstractIDFilter> filters, AggregationRegular regular,
                                 AggregationProduct aggregationProduct){

    if(StringUtils.isEmpty(data)){
      return null;
    }

    int id = -111;
    try{
      id = Integer.parseInt(data);
    }catch (Exception e){
      //e.printStackTrace();
      //System.out.println("invalid data:"+data);
      return null;
    }
    SimpleZSKU simpleZSKU = build(id);
    ///////////////////////////基本信息/////////////////////////////
    ProductSnapshot productSnapshot = MutableCaches.getInstance().P_SNAPSHOT.getCacheContent();
    //////////////////////////////indexMap//////////////////////////////////////////////////
    //Map<Integer, ProductProject> productProjectMap = productSnapshot.getProduct_image();
    IndexMap<ProductProject> productProjectMap = productSnapshot.getProduct_image();
    Map<Integer, String> poiInfo = productSnapshot.getPoiInfo();
    Map<Integer, String> tagInfo = productSnapshot.getTagInfo();
    ProductProject productProject;
    ///////////////////////////////////////////////////////////////
    //------------------------------过滤器--------------------------------
    if(productProjectMap.containsKey(id)){
      productProject = productProjectMap.get(id);
      simpleZSKU.setMarket_info(productProject.getMarketInfo());
    }else {
      return null;
    }
    //是否在售过滤器
    AbstractValidator validator = MutableCaches.getInstance()
                                               .SKU_AVAILABILITY_VALIDATOR
                                               .getCacheContent();
    if (!validator.isValid(spe, id)) {
      return null;
    }
    //其他过滤 上线时间，channel等
    if(filters != null) {
      for (int i = 0; i < filters.size(); i++) {
        if (!filters.get(i).isValid(productProject, spe))
          return null;
      }
    }
    /////////////////////////构建类别信息///////////////////////////
    //-------------------------依赖的基本信息----------------------------
    Map<Integer, Set<Integer>> vTypeMap = regular.getvTypeMap();
    Map<Integer, Set<Integer>> vTypeFilters = regular.getvTypeFilters();
    Map<Integer, Set<Integer>> vGTypeFilters = regular.getvGTypeFilters();
    String cityStr = regular.getCity();
    String countryStr = regular.getCountry();
    String continentName = regular.getContinent();
    int channel = regular.getChannel();
    Map<Integer, Map<Integer, VTYPEDES>> vTypeInfo = productProject.getZtagMap();
    List<Place> places = regular.getPlaces();
    Map<Integer, VTYPEDES> vTypeInfoForChannel;
    //-------------------------------------------------------------------
    if(vTypeInfo.containsKey(channel)){
      vTypeInfoForChannel = vTypeInfo.get(channel);
    }else {
      return null;
    }
    Iterator<Map.Entry<Integer, VTYPEDES>> iterator = vTypeInfoForChannel.entrySet().iterator();
    while (iterator.hasNext()){
      Map.Entry<Integer, VTYPEDES> entry = iterator.next();
      int t1 = entry.getKey();
      if(tagInfo.containsKey(t1)) {
        String tag_name = tagInfo.get(t1);
        VTYPEDES vtypedes = entry.getValue();
        if (vTypeMap.containsKey(t1)) {
          ///type1存在
          simpleZSKU.type1(t1);
          //STG INFO 增加
          STGInfo stgInfo;
          if (aggregationProduct.getStg_info().containsKey(t1)) {
            stgInfo = aggregationProduct.getStg_info().get(t1);
            stgInfo.incSize();
          } else {
            stgInfo = new STGInfo(1, tag_name);
            aggregationProduct.getStg_info().put(t1, stgInfo);
          }
          //----------------------stg对应的筛选项(topic)----------------------------//
          Set<Integer> hasFilters = new HashSet<>();
          Map<Integer, Set<Integer>> stg_filter_set = vtypedes.getSTG_FILTER_MAP();
          try{
            boolean isC = false;
            Iterator<Map.Entry<Integer, Set<Integer>>> itStgFilter = stg_filter_set.entrySet().iterator();
            long t11 = System.currentTimeMillis();
            while (itStgFilter.hasNext()){
              Map.Entry<Integer, Set<Integer>> e = itStgFilter.next();
              int categoryId = e.getKey();
              Set<Integer> tagIds = e.getValue();
              if(CollectionUtils.isNotEmpty(tagIds)){
                for(int tagId:tagIds){
                  if(tagInfo.containsKey(tagId)){
                    String name = tagInfo.get(tagId);
                    //////////////////////////////////////////
                    if (MapUtils.isNotEmpty(stgInfo.getTopic())) {
                      if (stgInfo.getTopic().containsKey(categoryId)) {
                        List<Topic> topics = stgInfo.getTopic().get(categoryId);
                        boolean isFoundTopic = false;
                        for (Topic topic : topics) {
                          if (topic.getId() == tagId) {
                            topic.incSize();
                            isFoundTopic = true;
                          }
                        }
                        if (!isFoundTopic) {
                          Topic topic = new Topic(tagId, name, 1);
                          topics.add(topic);
                        }
                      } else {
                        Topic topic = new Topic(tagId, name, 1);
                        List<Topic> list = new ArrayList<>();
                        list.add(topic);
                        stgInfo.getTopic().put(categoryId, list);
                      }
                    } else {
                      Map<Integer, List<Topic>> topicMap = new HashMap<>();
                      List<Topic> topicList = new ArrayList<>();
                      Topic topic = new Topic(tagId, name, 1);
                      topicList.add(topic);
                      topicMap.put(categoryId, topicList);
                      stgInfo.setTopic(topicMap);
                    }
                    //////////////////////////////////////////
                    isC = true;
                  }
                }
              }
              /////
              if(isC){
                hasFilters.add(categoryId);
              }
            }
            long t12 = System.currentTimeMillis();
            //System.out.println("STGTime:"+(t12-t11));
          }catch (Exception e){
            e.printStackTrace();
          }
          //--------------------------------------------目的地--------------------------------------//
          Map<Integer, Map<Integer, Set<Integer>>> destMap = productProject.getDestMap();
          Map<Integer, String> countryInfo = MutableCaches.getInstance().INVERSE_COUNTRIES.getCacheContent();
          Map<Integer, String> cityInfo = MutableCaches.getInstance().INVERSE_CITIES.getCacheContent();
          Set<Integer> no_travel_area = MutableCaches.getInstance().NO_TRAVEL_SET.getCacheContent();
          Map<Integer,Set<Integer>> area_info = MutableCaches.getInstance().AREA_CITY_MAP.getCacheContent();
          long t21 =System.currentTimeMillis();
          if(CollectionUtils.isNotEmpty(places)){
            for(Place place:places){
              try {
                int cityId = place.getCityId();
                int countryId = place.getCountryId();
                int continentId = place.getContinentId();
                ////////////具体到城市
                if (cityId > 0) {
                  Set<Integer> selectCitySet = new HashSet<>();
                  selectCitySet.add(cityId);
                  if(area_info.containsKey(cityId)){
                    Set<Integer> tmp = area_info.get(cityId);
                    if(CollectionUtils.isNotEmpty(tmp)){
                      selectCitySet.addAll(tmp);
                    }
                  }
                  if (destMap.containsKey(continentId)) {
                    Map<Integer, Set<Integer>> country_city_map = destMap.get(continentId);
                    if (country_city_map.containsKey(countryId)) {
                      List<City> cityList = new ArrayList<>();
                      Set<Integer> existCitySet = country_city_map.get(countryId);
                      for(int IdCity:selectCitySet){
                        if(existCitySet.contains(IdCity)){
                          if(!no_travel_area.contains(IdCity)){
                            String cityName = cityInfo.get(IdCity);
                            if(cityName != null) {
                              City city = new City(IdCity, cityName, 1);
                              cityList.add(city);
                            }
                          }
                        }
                      }
                      if(CollectionUtils.isNotEmpty(cityList)){
                        String countryName = countryInfo.get(countryId);
                        Country country = new Country(countryId, countryName, continentId,
                                                      cityList, 1);
                        ////////////////////////////////////////////////////////////////////////
                        if (CollectionUtils.isNotEmpty(stgInfo.getCountry())) {
                          boolean isFoundCou = false;
                          List<Country> countryList = stgInfo.getCountry();
                          for (Country country1 : countryList) {
                            if (country1.equals(country)) {
                              country1.add(country);
                              isFoundCou = true;
                              break;
                            }
                          }
                          if (!isFoundCou) {
                            countryList.add(country);
                          }
                        } else {
                          List<Country> countryList = new ArrayList<>();
                          countryList.add(country);
                          stgInfo.setCountry(countryList);
                        }
                        //////////////////////////////////////////////////////////////////////////

                      }
                    }
                  }

                  ///////////////////////////////////////////////////////////////////////////
                } else if(countryId > 0){////具体到国家
                  if(destMap.containsKey(continentId)){
                    if(destMap.get(continentId).containsKey(countryId)){
                      Set<Integer> existCitySet = destMap.get(continentId).get(countryId);
                      String countryName = countryInfo.get(countryId);
                      List<City> cityList = new ArrayList<>();
                      Country country = new Country(countryId, countryName, continentId, cityList, 1);

                      for(int IdCity:existCitySet){
                        if(!no_travel_area.contains(IdCity)){
                          String cityName = cityInfo.get(IdCity);
                          if(StringUtils.isNotEmpty(cityName)){
                            City city = new City(IdCity, cityName, 1);
                            cityList.add(city);
                          }
                        }
                      }
                      //////////////////////////////////////////////////////
                      if (CollectionUtils.isNotEmpty(stgInfo.getCountry())) {
                        boolean isFoundCou = false;
                        List<Country> countryList = stgInfo.getCountry();
                        for (Country country1 : countryList) {
                          if (country1.equals(country)) {
                            country1.add(country);
                            isFoundCou = true;
                            break;
                          }
                        }
                        if (!isFoundCou) {
                          countryList.add(country);
                        }
                      } else {
                        List<Country> countryList = new ArrayList<>();
                        countryList.add(country);
                        stgInfo.setCountry(countryList);
                      }
                      //////////////////////////////////////////////////////

                    }
                  }
                }else {//指定了大洲
                  if(destMap.containsKey(continentId)){
                    Map<Integer, Set<Integer>> country_city_map = destMap.get(continentId);
                    Iterator<Map.Entry<Integer, Set<Integer>>> itCountry = country_city_map.entrySet().iterator();
                    while (itCountry.hasNext()){
                      Map.Entry<Integer, Set<Integer>> entry1 = itCountry.next();
                      int Idcountry = entry1.getKey();
                      Set<Integer> existCitySet = entry1.getValue();
                      String nameCountry = countryInfo.get(Idcountry);
                      List<City> cityList = new ArrayList<>();
                      Country country = new Country(Idcountry, nameCountry, continentId, cityList, 1);
                      for(int IdCity:existCitySet){
                        if(!no_travel_area.contains(IdCity)){
                          String nameCity = cityInfo.get(IdCity);
                          if(StringUtils.isNotEmpty(nameCity)){
                            City city = new City(IdCity, nameCity, 1);
                            cityList.add(city);
                          }
                        }
                      }
                      ////////////////////////////////////////
                      if (CollectionUtils.isNotEmpty(stgInfo.getCountry())) {
                        boolean isFoundCou = false;
                        List<Country> countryList = stgInfo.getCountry();
                        for (Country country1 : countryList) {
                          if (country1.equals(country)) {
                            country1.add(country);
                            isFoundCou = true;
                            break;
                          }
                        }
                        if (!isFoundCou) {
                          countryList.add(country);
                        }
                      } else {
                        List<Country> countryList = new ArrayList<>();
                        countryList.add(country);
                        stgInfo.setCountry(countryList);
                      }
                      ////////////////////////////////////////
                    }
                  }
                }
                ////////////////////////////////////////////////
                ////////////////////
              }catch (Exception e){
                e.printStackTrace();
              }
            }
          }else {
            try {
              Set<Integer> continentSet = destMap.keySet();
              for (int continentId : continentSet) {
                Map<Integer, Set<Integer>> country_city_map = destMap.get(continentId);
                Iterator<Map.Entry<Integer, Set<Integer>>> itCountry = country_city_map.entrySet()
                                                                                       .iterator();
                while (itCountry.hasNext()) {
                  Map.Entry<Integer, Set<Integer>> entry1 = itCountry.next();
                  int Idcountry = entry1.getKey();
                  Set<Integer> existCitySet = entry1.getValue();
                  String nameCountry = countryInfo.get(Idcountry);
                  List<City> cityList = new ArrayList<>();
                  Country country = new Country(Idcountry, nameCountry, continentId, cityList, 1);
                  for(int IdCity:existCitySet){
                    if(!no_travel_area.contains(IdCity)){
                      String nameCity = cityInfo.get(IdCity);
                      if(StringUtils.isNotEmpty(nameCity)){
                        City city = new City(IdCity, nameCity, 1);
                        cityList.add(city);
                      }
                    }
                  }
                  ////////////////////////////////////////
                  if (CollectionUtils.isNotEmpty(stgInfo.getCountry())) {
                    boolean isFoundCou = false;
                    List<Country> countryList = stgInfo.getCountry();
                    for (Country country1 : countryList) {
                      if (country1.equals(country)) {
                        country1.add(country);
                        isFoundCou = true;
                        break;
                      }
                    }
                    if (!isFoundCou) {
                      countryList.add(country);
                    }
                  } else {
                    List<Country> countryList = new ArrayList<>();
                    countryList.add(country);
                    stgInfo.setCountry(countryList);
                  }
                  ////////////////////////////////////////
                }
              }
            }catch (Exception e){
              e.printStackTrace();
            }
          }
          long t22 = System.currentTimeMillis();
          //System.out.println("目的地Time:"+(t22 - t21));
          //-----------------------------poi--------------------------------------//
          List<Integer> pois = productProject.getPoiList();
          if (CollectionUtils.isNotEmpty(stgInfo.getPoi())) {
            boolean isFoundCou = false;
            List<Poi> poiList = stgInfo.getPoi();
            List<Poi> poiList1 = new ArrayList<>(poiList);
            for (int poiId : pois) {
              for (Poi poi : poiList1) {
                if (poi.getId() == poiId) {
                  poi.incSize();
                  isFoundCou = true;
                  break;
                }
              }
              if (!isFoundCou) {
                if(poiInfo.containsKey(poiId)) {
                  Poi poiE = new Poi(poiId, poiInfo.get(poiId), 1);
                  poiList.add(poiE);
                }
              }
            }
          }else{
            List<Poi> poiList = new ArrayList<>();
            for(int poiId:pois){
              if(poiInfo.containsKey(poiId)){
                Poi poi = new Poi(poiId, poiInfo.get(poiId), 1);
                poiList.add(poi);
              }
            }
            stgInfo.setPoi(poiList);
          }
          //-----------------------------abroad-----------------------------------//
          List<Integer> setOutPlaceList = productProject.getSetOutPlace();
          for(int setId:setOutPlaceList) {
            SetOutPlace setOutPlace1 = new SetOutPlace(Constants.START_POS_MAP_INVERS_NAME.get(setId)
              , Constants.START_POS_MAP_INVERS.get(setId), 1);
            List<SetOutPlace> setoutList = stgInfo.getAbroad();
            if (CollectionUtils.isNotEmpty(setoutList)) {
              boolean isFound = false;
              for (SetOutPlace city1 : setoutList) {
                if (city1.equals(setOutPlace1)) {
                  city1.add(setOutPlace1);
                  isFound = true;
                  break;
                }
              }
              if (!isFound) {
                setoutList.add(setOutPlace1);
              }
            } else {
              List<SetOutPlace> list = new ArrayList<>();
              list.add(setOutPlace1);
              stgInfo.setAbroad(list);
            }
          }
          //-----------------------------startcity--------------------------------//
          List<Integer> start_city_list = productProject.getStartCity();
          for(int cityId:start_city_list) {
            List<City> cityList = stgInfo.getStart_city_id();
            if (CollectionUtils.isNotEmpty(cityList)) {
              boolean isFound = false;
              for (City city1 : cityList) {
                if (city1.getId() == cityId) {
                  city1.inc();
                  isFound = true;
                  break;
                }
              }
              if (!isFound) {
                if(cityInfo.containsKey(cityId)) {
                  City city = new City(cityId, cityInfo.get(cityId), 1);
                  cityList.add(city);
                }
              }
            } else {
              if(cityInfo.containsKey(cityId)) {
                City city = new City(cityId, cityInfo.get(cityId), 1);
                List<City> list = new ArrayList<>();
                list.add(city);
                stgInfo.setStart_city_id(list);
              }
            }
          }
          //-----------------------------travelday--------------------------------//
          int day = productProject.getTravel_day();
          if( day > 0 ){
            TravelDay travelDay = new TravelDay(day, 1);
            List<TravelDay> travelDays = stgInfo.getTravelDays();
            if (CollectionUtils.isNotEmpty(travelDays)) {
              boolean isFound = false;
              for (TravelDay td : travelDays) {
                if (td.equals(travelDay)) {
                  td.add(travelDay);
                  isFound = true;
                  break;
                }
              }
              if (!isFound) {
                travelDays.add(travelDay);
              }
            } else {
              List<TravelDay> list = new ArrayList<>();
              list.add(travelDay);
              stgInfo.setTravelDays(list);
            }
          }
          //-----------------------------stype------------------------------------//
          Set<Integer> stypeSet = vTypeMap.get(t1);
          Set<Integer> exSet = vtypedes.getST_FILTER_MAP().keySet();
          for(int type2Id:exSet){
            if(stypeSet.contains(type2Id)){
              simpleZSKU.type2(type2Id);
              String tName = tagInfo.get(type2Id);
              //if(tagInfo)
              ///////////////////////////////////////////////////////////////////////////////
              if (CollectionUtils.isNotEmpty(stgInfo.getS_type())) {
                boolean isFound = false;
                for (VStype vStype : stgInfo.getS_type()) {
                  if (vStype.getId() == type2Id) {
                    isFound = true;
                    vStype.incSize();
                    break;
                  }
                }
                if (!isFound) {
                  VStype vStype = new VStype(type2Id, tName, 1);
                  stgInfo.getS_type().add(vStype);
                }
              } else {
                VStype vStype = new VStype(type2Id, tName, 1);
                List<VStype> stypes = new ArrayList<>();
                stypes.add(vStype);
                stgInfo.setS_type(stypes);
              }
              //////////////////////////////////////////////////////////////////////////////
              //筛选项
              Map<Integer, Set<Integer>> stypeFilterMap = vtypedes.getST_FILTER_MAP().get(type2Id);
              if(MapUtils.isNotEmpty(stypeFilterMap)){
                Set<Integer> categorySet = stypeFilterMap.keySet();
                for(int categoryId:categorySet){
                  if(!hasFilters.contains(categoryId)){
                    Set<Integer> tagIds = stypeFilterMap.get(categoryId);
                    if(CollectionUtils.isNotEmpty(tagIds)){
                      for(int tagId:tagIds){
                        if(tagInfo.containsKey(tagId)){
                          String name = tagInfo.get(tagId);
                          //////////////////////////////////////////
                          if (MapUtils.isNotEmpty(stgInfo.getTopic())) {
                            if (stgInfo.getTopic().containsKey(categoryId)) {
                              List<Topic> topics = stgInfo.getTopic().get(categoryId);
                              boolean isFoundTopic = false;
                              for (Topic topic : topics) {
                                if (topic.getId() == tagId) {
                                  topic.incSize();
                                  isFoundTopic = true;
                                }
                              }
                              if (!isFoundTopic) {
                                Topic topic = new Topic(tagId, name, 1);
                                topics.add(topic);
                              }
                            } else {
                              Topic topic = new Topic(tagId, name, 1);
                              List<Topic> list = new ArrayList<>();
                              list.add(topic);
                              stgInfo.getTopic().put(categoryId, list);
                            }
                          } else {
                            Map<Integer, List<Topic>> topicMap = new HashMap<>();
                            List<Topic> topicList = new ArrayList<>();
                            Topic topic = new Topic(tagId, name, 1);
                            topicList.add(topic);
                            topicMap.put(categoryId, topicList);
                            stgInfo.setTopic(topicMap);
                          }
                          //////////////////////////////////////////
                        }
                      }
                    }
                  }
                }
              }
            }
          }
          //-------------------------stype对应的筛选项(topic)-----------------------//


        }
      }else {
        //fix 注释需要去掉
        System.out.println("TYPE1:"+t1+" miss name~");
      }
    }
    //--------------------------------------------------------------------
    return simpleZSKU;

  }

  public List<SimpleVSKU> buildVSKUS(){
    List<SimpleVSKU> simpleVSKUs = new ArrayList<>(this.type1.size());
    for(int t1:this.type1){
      simpleVSKUs.add(new SimpleVSKU(this.id, t1, 0, this.type2, market_info, title, null, null));
    }
    if(CollectionUtils.isEmpty(simpleVSKUs)) {
      System.out.println("buildVSKUS:" + this.id);
      System.out.println("empty:" + this.type2);
    }
    return simpleVSKUs;
  }

  /////////////////////////////////////////////////////////////////
  public static SimpleZSKU build(SearchParameterEntity spe, String data,
                                 List<AbstractIDFilter> filters, AggregationRegular regular,
                                 Set<Integer> stgSet) {

    if (StringUtils.isEmpty(data)) {
      return null;
    }

    int id = -111;
    try {
      id = Integer.parseInt(data);
    } catch (Exception e) {
      //e.printStackTrace();
      //System.out.println("invalid data:"+data);
      return null;
    }
    SimpleZSKU simpleZSKU = build(id);
    ///////////////////////////基本信息/////////////////////////////
    ProductSnapshot productSnapshot = MutableCaches.getInstance().P_SNAPSHOT.getCacheContent();
    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    //Map<Integer, ProductProject> productProjectMap = productSnapshot.getProduct_image();
    IndexMap<ProductProject> productProjectMap = productSnapshot.getProduct_image();
    Map<Integer, String> poiInfo = productSnapshot.getPoiInfo();
    Map<Integer, String> tagInfo = productSnapshot.getTagInfo();
    ProductProject productProject;
    ///////////////////////////////////////////////////////////////
    //------------------------------过滤器--------------------------------
    if (productProjectMap.containsKey(id)) {
      productProject = productProjectMap.get(id);
      simpleZSKU.setMarket_info(productProject.getMarketInfo());
    } else {
      return null;
    }
    //是否在售过滤器
    AbstractValidator validator = MutableCaches.getInstance().SKU_AVAILABILITY_VALIDATOR
      .getCacheContent();
    if (!validator.isValid(spe, id)) {
      return null;
    }
    //其他过滤 上线时间，channel等
    if (filters != null) {
      for (int i = 0; i < filters.size(); i++) {
        if (!filters.get(i).isValid(productProject, spe))
          return null;
      }
    }
    /////////////////////////构建类别信息///////////////////////////
    //-------------------------依赖的基本信息----------------------------
    Map<Integer, Set<Integer>> vTypeMap = regular.getvTypeMap();
    int channel = regular.getChannel();
    Map<Integer, Map<Integer, VTYPEDES>> vTypeInfo = productProject.getZtagMap();
    Map<Integer, VTYPEDES> vTypeInfoForChannel;
    //-------------------------------------------------------------------
    if (vTypeInfo.containsKey(channel)) {
      vTypeInfoForChannel = vTypeInfo.get(channel);
    } else {
      return null;
    }
    Iterator<Map.Entry<Integer, VTYPEDES>> iterator = vTypeInfoForChannel.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry<Integer, VTYPEDES> entry = iterator.next();
      int t1 = entry.getKey();
      if (tagInfo.containsKey(t1)) {
        String tag_name = tagInfo.get(t1);
        VTYPEDES vtypedes = entry.getValue();
        if (vTypeMap.containsKey(t1)) {
          ///type1存在
          simpleZSKU.type1(t1);
          stgSet.add(t1);
          Set<Integer> type2Set = vTypeMap.get(t1);
          Set<Integer> exType2Set = vtypedes.getST_FILTER_MAP().keySet();
          for(int type2Id:type2Set){
            if(exType2Set.contains(type2Id)){
              simpleZSKU.type2(type2Id);
            }
          }
        }
      }
    }

    return simpleZSKU;
  }
}
